#include "aptablemodel.h"
#include <QBrush>

ApTableModel::ApTableModel(QObject *parent) :
    QAbstractTableModel(parent)
{

}

void ApTableModel ::load_data(const QVariantList &qvl_h_indexes)
{
    if(mp_dbms==NULL) return;
    mqvl_ap_indexes.clear();
    QVariantList     qvl_indexes;
    for(int i = 0; i < qvl_h_indexes.size(); i++)
    {
        qvl_indexes.clear();
        qvl_indexes = mp_dbms->get_db(LAKDB_APARTMENTS)->findDataEq(FLD_APDB_HSINDEX, qvl_h_indexes.at(i));
        mqvl_ap_indexes.append(qvl_indexes);
    }
    qDebug()<<"ApTableModel. Model loaded. size"<<mqvl_ap_indexes.size();
}

int ApTableModel::rowCount(const QModelIndex &/*parent*/) const
{
    //if(parent.isValid()) return 0;
    return mqvl_ap_indexes.size();
}

int ApTableModel::columnCount(const QModelIndex &/*parent*/) const
{
  //  if(parent.isValid()) return 0;
    return 6;

}

QVariant ApTableModel::data(const QModelIndex &index, int role) const
{

   static int last_row = -1;
   static SLEE_HDB_REC qh_data;
    int r = index.row();
    int c = index.column();
    if(role == Qt::BackgroundRole)
    {
        if(mqv_active_h_index == qh_data.value(FLD_APDB_HSINDEX))
        {
            if(r%2)
                    return QVariant(QBrush(QColor(229,229,253)));
            return QVariant(QBrush(QColor(235, 235, 248)));
        }
    }
    if(role!= Qt::DisplayRole) return QVariant();
    if(last_row != r)
    {
        qh_data = mp_dbms->get_data(LAKDB_APARTMENTS,mqvl_ap_indexes.at(r));
        last_row = r;
    }
    QString str;
    QVariantHash qvh_spaces;
    switch(c)
    {
    case 0: str = mp_dbms->get_house_addr(qh_data.value(FLD_APDB_HSINDEX));  //house address
                    break;
    case 1: str = qh_data.value(FLD_APDB_NUMBER).toString();  //apartment number
                    break;
    case 2: str= qh_data.value(FLD_APDB_RCOUNT).toString();
                    break;
    case 3: str=qh_data.value(FLD_APDB_TSPC).toString();
                    break;
    case 4:
                qvh_spaces = qh_data.value(FLD_APDB_LSPSHASH).toHash();
                str= QString::number(mp_dbms->get_apartment_lspace(qvh_spaces));
                    break;
    case 5: str=qh_data.value(FLD_APDB_COMMENTS).toString();
                    break;
    }
    return QVariant(str);

}


QVariant ApTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role!=Qt::DisplayRole) return QVariant();
    if(orientation == Qt::Vertical) return QVariant();
    QString str;
    switch(section)
    {
    case 0: str = RUSTR("Адрес");  //house address
                    break;
    case 1: str = RUSTR("Номер"); //apartment number
                    break;
    case 2: str = RUSTR("Кол-во\nкомнат");
                    break;
    case 3: str = RUSTR("Общая\nпл.(кв.м.)");
                    break;
    case 4: str = RUSTR("Жилая\nпл.(кв.м.)");
                    break;
    case 5: str = RUSTR("Комментарии");
                    break;
    }
    return QVariant(str);
}

QModelIndex ApTableModel::get_model_index_by_h_index(const QVariant &qv_h_index)
{
    if(!qv_h_index.isValid())    return QModelIndex();
    SLEE_HDB_REC qh_data;
    for(int i = 0; i <mqvl_ap_indexes.size(); i++)
        {
            qh_data = mp_dbms->get_data(LAKDB_APARTMENTS, mqvl_ap_indexes.at(i));
            if(qh_data.value(FLD_APDB_HSINDEX) == qv_h_index)
                return createIndex(i, 0);
        }
    return QModelIndex();
}

QVariant ApTableModel::get_ap_db_index(const QModelIndex &index)
{
    if(!index.isValid()) return QVariant();
    return mqvl_ap_indexes.at(index.row());
}

  SLEE_HDB_REC ApTableModel::get_ap_data(const QModelIndex &index)
  {
        if(!index.isValid()) return SLEE_HDB_REC();
        return  mp_dbms->get_data(LAKDB_APARTMENTS, mqvl_ap_indexes.at(index.row()));
  }

  void ApTableModel::update_data(const QModelIndex &/*index*/, const SLEE_HDB_REC &/*qh_data*/)
  {
        //mqhl_apartments[index.row()] = qh_data;
  }


QModelIndex ApTableModel::get_model_index_by_ap_index(const QVariant &qv_ap_index)
{
    if(!qv_ap_index.isValid()) return QModelIndex();
    int row = mqvl_ap_indexes.indexOf(qv_ap_index);
    if(row == -1) return QModelIndex();
    return createIndex(row,0);
}

int ApTableModel::get_fld_by_mindex(const QModelIndex &index)
{
    switch(index.column())
    {
    case 2: return FLD_APDB_RCOUNT;
    case 3: return FLD_APDB_TSPC;
    case 4: return FLD_APDB_NUMBER;
    case 5: return FLD_APDB_NUMBER;
    case 6: return FLD_APDB_COMMENTS;
    };
    return FLD_APDB_NUMBER;
}
